//
//  ThemeImageView.m
//  Project_2_weibo
//
//  Created by 林林 on 16/6/17.
//  Copyright © 2016年 lhn. All rights reserved.
//

#import "ThemeImageView.h"
#import "ThemeManager.h"

@implementation ThemeImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme) name:kThemeChangedNotificationName object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme) name:kThemeChangedNotificationName object:nil];

}

- (void)changeTheme {
    
    //获取相应主题图片
    UIImage *image = [[ThemeManager sharedManager] themeImageWithImageName:self.imageName];
    //图片拉伸
    if (_leftCapWidth != 0 || _topCapHeight != 0) {
        
        image = [image stretchableImageWithLeftCapWidth:_leftCapWidth topCapHeight:_topCapHeight];
    }
    self.image = image;
}

- (void)setImageName:(NSString *)imageName {
    
    _imageName = [imageName copy];
    
    [self changeTheme];
}

- (void)setLeftCapWidth:(CGFloat)leftCapWidth {
    
    _leftCapWidth = leftCapWidth;
    
    [self changeTheme];
}

- (void)setTopCapHeight:(CGFloat)topCapHeight {
    
    _topCapHeight = topCapHeight;
    
    [self changeTheme];
}

@end
